package de.baumann.browser.model

import android.util.Log
import de.baumann.browser.BuildConfig
import de.baumann.browser.api.net.FuckGGApiService
import de.baumann.browser.api.net.GGApiService
import de.baumann.browser.api.net.base.ApiBuilder
import de.baumann.browser.api.net.vo.Result
import de.baumann.browser.api.net.vo.gg.*
import de.baumann.browser.rx.RxSchedulers
import de.baumann.browser.utils.web3j.Erc20Util
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import java.lang.Exception
import java.math.BigDecimal

private val ggBApiService: GGApiService = ApiBuilder.buildService(GGApiService::class.java)
private val fuckGGApiService: FuckGGApiService = ApiBuilder.buildService(FuckGGApiService::class.java)

fun create(name: String, address: String, hash: String): Observable<Result<Any>> {
    return ggBApiService.createSmeltery(name, address, hash, "200000", "12")
            .compose(RxSchedulers.ioMain())
}


fun join(smelteryId: String, address: String, hash: String, qvlaue: String, time: String): Observable<Result<Any>> {
    return ggBApiService.joinSmeltery(smelteryId, address, hash, qvlaue, time)
            .compose(RxSchedulers.ioMain())
}

fun prepositionByAddress(address: String): Observable<Result<GGMain>> {
    return ggBApiService.prepositionByAddress(address).compose(RxSchedulers.ioMain())
}

fun getPledgeRule(type: String): Observable<Result<GGRule>> {
    return ggBApiService.getPledgeRule(type).compose(RxSchedulers.ioMain())
}


fun rank(): Observable<Result<List<Smelter>>> {
    return ggBApiService.smelteryRank().compose(RxSchedulers.ioMain())
}


fun morgageRecord(address: String): Observable<Result<List<Morgage>>> {
    return ggBApiService.morgageRecord(address).compose(RxSchedulers.ioMain())
}


fun airdropRecord(address: String): Observable<Result<List<Morgage>>> {
    return fuckGGApiService.airdropRecord(address, "gung").compose(RxSchedulers.ioMain())
}


fun recordTotal(address: String): Observable<Result<XXYY>> {
    return ggBApiService.recordTotal(address).compose(RxSchedulers.ioMain())
}

fun redemption(address: String, profit: String, hash: String): Observable<Result<Any>> {
    return ggBApiService.redemption(address, profit, hash).compose(RxSchedulers.ioMain())
}

fun getExtractTrans(address: String): Observable<Result<List<ExtractTrans>>> {
    return ggBApiService.getExtractTrans(address).compose(RxSchedulers.ioMain())
}

fun selectSameName(name:String):Observable<Result<F>>{
    return ggBApiService.selectSameName(name).compose(RxSchedulers.ioMain())
}

fun getPledgeNumAndEarnings(address: String, id: String): Observable<Result<XXYY>> {
    return ggBApiService.getPledgeNumAndEarnings(address, id).compose(RxSchedulers.ioMain())
}

fun getPledgeLog(id: String): Observable<Result<List<Morgage>>> {
    return ggBApiService.getPledgeLog(id).compose(RxSchedulers.ioMain())
}

fun endTime(address: String) {
    Observable.create<Long> {
        Erc20Util.endTime(BuildConfig.WALLET_NODE_URL, BuildConfig.GG_ADDRESS, address)
    }.compose(RxSchedulers.ioMain())
            .subscribe(object : Observer<Long> {
                override fun onComplete() {
                }

                override fun onSubscribe(d: Disposable) {
                }

                override fun onNext(t: Long) {
                    Log.d("````", "onNext: " + t)
                }

                override fun onError(e: Throwable) {
                    e.printStackTrace()
                }

            })
}


fun userDepositsNum(smelterAddress: String, address: String): Observable<BigDecimal> {
    return Observable.create<BigDecimal> {
        try {
            val bigDecimal = Erc20Util.userDepositsNum(BuildConfig.WALLET_NODE_URL, BuildConfig.GG_ADDRESS, smelterAddress, address)
            it.onNext(bigDecimal)
        } catch (e: Exception) {
            it.onError(e)
        }
    }.compose(RxSchedulers.ioMain())
}

fun ownerUserCounts(smelterAddress: String): Observable<BigDecimal> {
    return Observable.create<BigDecimal> {
        try {
            val bigDecimal = Erc20Util.ownerUserCounts(BuildConfig.WALLET_NODE_URL, BuildConfig.GG_ADDRESS, smelterAddress)
            it.onNext(bigDecimal)
        } catch (e: Exception) {
            it.onError(e)
        }
    }.compose(RxSchedulers.ioMain())
}

fun userWithdraw(userAddress: String, amount: String, privateKey: String): Observable<String> {
    return Observable.create<String> {
        try {
            val hash = Erc20Util.userWithdraw2(BuildConfig.WALLET_NODE_URL, userAddress, amount, privateKey, BuildConfig.GG_ADDRESS)
            it.onNext(hash)
        } catch (e: Exception) {
            e.printStackTrace()
            it.onError(e)
        }
    }.compose(RxSchedulers.ioMain())
}


fun ownerWithdraw(userAddress: String, privateKey: String): Observable<String> {
    return Observable.create<String> {
        try {
            val hash = Erc20Util.ownerWithdraw(BuildConfig.WALLET_NODE_URL, userAddress, privateKey, BuildConfig.GG_ADDRESS)
            it.onNext(hash)
        } catch (e: Exception) {
            e.printStackTrace()
            it.onError(e)
        }
    }.compose(RxSchedulers.ioMain())
}





